//
//  NewViewController.m
//  网易新闻
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewViewController.h"
#import "Duanzi.h"
#import "NewTableViewCell.h"
#import "ImageTableViewCell.h"
#import "Highet.h"
@interface NewViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)NSMutableArray *muarray;


@end

@implementation NewViewController

- (void)viewDidLoad {
    
    self.muarray = [[NSMutableArray alloc]init];
    

    
    [super viewDidLoad];
    [self creactTabview];
    [self creashuyju];
    
}

-(void)creactTabview{
    UITableView *tablieview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    tablieview.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:tablieview];
    tablieview.delegate = self;
    tablieview.dataSource = self;
    tablieview.rowHeight  = 100;
    
    [tablieview registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [tablieview registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"imcell"];
    
    
    
}
-(void)creashuyju{
    NSString *str=[[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@".txt"];
    NSData *data = [NSData dataWithContentsOfFile:str];
    
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error ];
    NSArray *array = [dic objectForKey:@"段子"];
    for(NSDictionary *dict in array){
        Duanzi *duanzi = [[Duanzi alloc]init];
        
        [duanzi setValuesForKeysWithDictionary:dict];
        [self.muarray addObject:duanzi];
        [duanzi release];
    }

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.muarray.count;
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Duanzi *duan =[self.muarray objectAtIndex:indexPath.row];
    if (duan.imgsrc) {
        ImageTableViewCell *icell = [tableView dequeueReusableCellWithIdentifier:@"imcell"];
    icell.duanzi = duan;
        return icell;
    }else{
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    
    cell.duanzi = duan;
    
    return cell;
    
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     Duanzi *duan =[self.muarray objectAtIndex:indexPath.row];
    if (duan.imgsrc) {
      
        return [Highet imageHeightWithUrlstring:duan.imgsrc]+40;
    }
    return [Highet heightWith:duan.digest]+20;



}


@end
